package com.stusystem.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.stusystem.dao.UserDao;
import com.stusystem.entity.Userbean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping(value = "user")
public class UserController {
    @Autowired
    private UserDao userDao;

    //	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
//	UserDao userDao = (UserDao) applicationContext.getBean("userDao");
    //返回登陆界面
    //验证用户的用户名和密码是有和数据库中的匹配
    @RequestMapping(value = {"/login"})
    public String userlogin() {
        return "login";
    }

    //登陆验证
    @RequestMapping(value = {"/loginyanzheng"})
    public void loginyanzheng(Userbean userbean, HttpServletResponse response, HttpServletRequest request) throws IOException {
        final String code = request.getParameter("code");

        final String sessionCode = Convert.toStr(request.getSession().getAttribute("code"));


        if (ObjectUtil.isEmpty(code) || ObjectUtil.isEmpty(sessionCode)) {
            response.getWriter().println("{\"status\":0,\"msg\":\"验证码错误！\"}");
        } else if (!code.equals(sessionCode)) {
            response.getWriter().println("{\"status\":0,\"msg\":\"验证码错误！\"}");
        } else {
            Userbean user = userDao.getUsrInfoByNameAndPsw(userbean);
            if (user == null) {
                response.getWriter().println("{\"status\":0,\"msg\":\"用户名或密码有误！\"}");
            } else {
                // 用户的信息存放到session中。
                HttpSession session = request.getSession();
                session.setAttribute("userbean", user);
                response.getWriter().println("{\"status\":1,\"url\":\"index\"}");
            }
        }
    }


    @RequestMapping(value = {"/createCode"})
    public void createCode(HttpServletResponse response, HttpServletRequest request) {

        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        captcha.setGenerator(randomGenerator);
// 重新生成code
        captcha.createCode();

        try (ServletOutputStream outputStream = response.getOutputStream()) {
            captcha.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        request.getSession().setAttribute("code", captcha.getCode());
    }

    //返回系统主界面
    @RequestMapping(value = {"/index"})
    public String index() {
        return "index";
    }

    //返回关于页面
    @RequestMapping(value = {"/gy"})
    public String guanyu() {
        return "gy";
    }

    //返回密码修改页面
    @RequestMapping(value = {"/dlmmxg"})
    public String dlmmxg() {
        return "dlmmxg";
    }

    //修改登录密码
    @RequestMapping(value = {"/mmxg"})
    public String mmxg(Userbean userbean, HttpServletResponse response, HttpServletRequest request) {
        Userbean user = userDao.getUsrInfoByNameAndPsw(userbean);
        if (user == null) {
            request.setAttribute("status", '0');
        } else {
            userDao.mmxg(userbean);
            request.setAttribute("status", '1');
        }
        return "dlmmxg";
    }

    //退出系统
    @RequestMapping(value = {"/loginout"})
    public String loginout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return "login";
    }
}
